<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>查询客房页面</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" href="/css/font.css">
    <link rel="stylesheet" href="/css/xadmin.css">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript" src="/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/js/xadmin.js"></script>
    <script type="text/javascript" src="/js/cookie.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
<div class="x-nav">
      <span class="layui-breadcrumb">
        <a href="">首页</a>
        <a href="">演示</a>
        <a>
          <cite>导航元素</cite></a>
      </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:30px">刷新</i></a>
</div>
<div class="x-body">
    <div class="layui-row">
        <div class="demoTable">
            <button id="add" class="layui-btn add" lay-filter="add" data-type="add"><i class="layui-icon"></i>增加房间
            </button>
        </div>
    </div>
    <div class="layui-row">
        <form class="layui-form layui-col-md12  layui-form-pane">
            <div class="layui-form-item" id="x-city">
                <div class="layui-input-inline">
                    <select name="rtid" id="roomType">
                        <option value="">请选择类型</option>
                    </select>
                </div>
                <div class="layui-input-inline">
                    <input type="number" name="roomNumber" id="roomNumber"
                           placeholder="输入房号" autocomplete="off"
                           class="layui-input" >
                </div>
                <div class="layui-input-inline">
                    <select name="curStatus" id="curStatus">
                        <option value="">房间状态</option>
                        <option value="0">空房</option>
                        <option value="1">入住</option>
                        <option value="2">维修</option>
                        <option value="3">废弃</option>
                    </select>
                </div>
                <button type="button" class="layui-btn" lay-submit lay-filter="search"><i class="layui-icon">&#xe615;</i></button>
            </div>
        </form>
        <input type="hidden" id="selectValue"/>
        <input type="hidden" id="clockRoomValue"/>
        <input type="hidden" id="curRoomType"/>
        <table class="layui-table" id="r_table" lay-filter="test">
        </table>
        <div class="page">
            <div>
                <a class="prev" href="">&lt;&lt;</a>
                <a class="num" href="">1</a>
                <span class="current">2</span>
                <a class="num" href="">3</a>
                <a class="num" href="">489</a>
                <a class="next" href="">&gt;&gt;</a>
            </div>
        </div>
    </div>
</div>

<script>
    window.onload = function () {
        layui.use('laydate', function () {
            var laydate = layui.laydate;
            //执行一个laydate实例
            laydate.render({
                elem: '#start' //指定元素
            });
            //执行一个laydate实例
            laydate.render({
                elem: '#end' //指定元素
            });
        });
        //获取表格中指定列的值传递给新修改弹窗
        layui.use(['layer', 'table', 'form', 'jquery'], function () {
            var $ = layui.jquery,
                layer = layui.layer,
                form = layui.form;

            var table = layui.table;
            //根据返回的房间类型设置下拉框中房间类型的值
            var roomType;
            $.get({
                url: '/roomType/list',
                dataType: 'json',
                success: function (res) {
                    console.log(res);
                    roomType = res.data;
                    var selectType = $("#roomType");
                    roomType.forEach(function (ele, index) {
                        var option = $('<option></option>');
                        //如果该类型在下拉框中没有，则添加
                        if ($('#roomType option[value=' + ele['id'] + ']').length == 0) {
                            option.val(ele['id']);
                            option.text(ele['roomType']);
                            option.appendTo(selectType);
                        }
                        form.render('select');
                    });

                }
            });
            //渲染表格
            var tableIns = table.render({
                id: 'tableId'
                , elem: '#r_table'
                , height: 400
                , url: '/roomDetail/list' //数据接口
                , toolbar: '#toolbarDemo'
                , parseData: function (res) {
                    console.log(res);
                    var data = res.data;
                    for (var index in data) {
                        switch (data[index]["curStatus"]) {
                            case 0:
                                data[index]["curStatus"] = "空闲";
                                break;
                            case 1:
                                data[index]["curStatus"] = "入住";
                                break;
                            case 2:
                                data[index]["curStatus"] = "维修";
                                break;
                            case 3:
                                data[index]["curStatus"] = "废弃";
                                break;
                        }
                        switch (data[index]["clockRoom"]) {
                            case 0:
                                data[index]["clockRoom"] = "否";
                                break;
                            case 1:
                                data[index]["clockRoom"] = "是";
                                break;
                        }
                    }
                    return {
                        "code": res.status, //解析接口状态
                        "msg": res.msg, //解析提示文本
                        "count": res.total, //解析数据长度
                        "data": data //解析数据列表
                    }
                }
                , page: true //开启分页
                , cols: [
                    [ //表头
                        {type: 'checkbox',width:"7%", fixed: 'left'},
                        {field: 'roomNumber', title: '房间号', width: '13%', sort: true, fixed: 'left', align: 'center'}
                        , {field: 'roomType', title: '房间类型', width: '13%', sort:true, align: 'center'}
                        , {field: 'clockRoom', title: '钟点房状态', width: '13%', align: 'center'}
                        , {field: 'clockPrice', title: '钟点房价格\\小时', width: '13%', sort: true, align: 'center'}
                        , {field: 'roomTel', title: '电话', width: '15%', align: 'center'}
                        , {field: 'curStatus', title: '状态', width: '13%', align: 'center'}
                        , {field: 'roomEdit', title: '操作', toolbar: '#edit', width: '13%', align: 'center'}
                    ]
                ]
                , done: function (res, curr, count) {
                    console.log(res);
                    table.on('tool(test)', function (obj) {
                        //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
                        //console.log(obj);
                        var data = obj.data; //获得当前行数据
                        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
                        var curStatus;
                        var clockRoom;
                        switch (data["curStatus"]) {
                            case "空闲":
                                curStatus = 0;
                                break;
                            case "入住":
                                curStatus = 1;
                                break;
                            case "维修":
                                curStatus = 2;
                                break;
                            case "废弃":
                                curStatus = 3;
                                break;
                        }
                        switch (data["clockRoom"]) {
                            case "否":
                                clockRoom = 0;
                                break;
                            case "是":
                                clockRoom = 1;
                                break;
                        }
                        //获取当前房间类型
                        var curType = data['rtid'];
                        $("#selectValue").val(curStatus);
                        $("#clockRoomValue").val(clockRoom);
                        //当前选中行对应的房间类型
                        $("#curRoomType").val(curType);
                        //当要触发修改事件的时候，传递data数据
                        if (layEvent == "edit") {
                            editRoom('修改', '/roomDetail/roomEdit', 500, 400, data);
                        }
                        else if (layEvent === 'del') { //删除
                            layer.confirm('确定删除吗？', function (index) {
                                $.post("/roomDetail/remove", {'ids': data.roomNumber}, function (res) {
                                    console.log(obj);
                                    if (res.status == 0) {
                                        obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                                        layer.alert("删除成功", {icon: 6})
                                    }
                                    else {
                                        layer.alert("删除失败", {icon: 5})
                                    }
                                });
                                layer.close(index);
                                //向服务端发送删除指令
                            });
                        }
                    });
                }

            });
            form.on('submit(search)', function (data){
                console.log("search data",data);
                tableIns.reload({
                    where:data.field
                });
            });
            //修改房间信息弹窗
            function editRoom(title, url, w, h, data) {
                //data为选中行的房间信息
                if (title == null || title == '') {
                    title = false;
                }
                ;
                if (url == null || url == '') {
                    url = "404.html";
                }
                ;
                if (w == null || w == '') {
                    w = ($(window).width() * 0.9);
                }
                ;
                if (h == null || h == '') {
                    h = ($(window).height() - 50);
                }
                ;
                var index = layer.open({
                    type: 2,
                    area: [w + 'px', h + 'px'],
                    fix: false, //不固定
                    maxmin: true,
                    shadeClose: true,
                    shade: 0.4,
                    title: title,
                    content: url,
                    success: function (layero, index) {
                        //在新窗口保留之前的值，在有值的基础上修改,body为子窗口
                        var body = layer.getChildFrame('body', index);
                        //表格渲染后获取指定行数据
                        body.find("#roomNumber").val(data["roomNumber"]);
                        body.find("#roomType").val(data["roomType"]);
                        body.find("#clockRoom").val(data["clockRoom"]);
                        body.find("#clockPrice").val(data["clockPrice"]);
                        body.find("#roomTel").val(data["roomTel"]);
                    }
                });
            }
            table.on("toolbar(test)",function (obj) {
                console.log(obj);
                var checkStatus = table.checkStatus(obj.config.id);
                if(obj.event=='delAll'){
                    layer.confirm('确定删除吗？',function () {
                        var arr=[];
                        console.log(checkStatus);
                        $.each(checkStatus.data, function (idx, obj) {
                            //获得所有id
                            arr.push(obj.roomNumber);
                        });
                        console.log(arr.toString());
                        // 异步操作
                        $.post("/roomDetail/remove", {'ids': arr.toString()}, function (res) {
                            if (res.status == 0) {
                                layer.msg("删除成功", {icon: 6})
                                tableIns.reload();
                            }
                            else {
                                layer.alert("修改失败", {icon: 5})
                            }
                        });
                        layer.close(index);
                    })
                }
            });

            //增加房间信息弹窗
            function add(title, w, h, url, roomType) {
                if (title == null || title == '') {
                    title = false;
                }
                ;
                if (url == null || url == '') {
                    url = "404.html";
                }
                ;
                if (w == null || w == '') {
                    w = ($(window).width() * 0.9);
                }
                ;
                if (h == null || h == '') {
                    h = ($(window).height() - 50);
                }
                ;
                layer.open({
                    type: 2,
                    area: [w + "px", h + "px"],
                    fix: false,
                    shade: 0.3,
                    title: title,
                    shadeClose: true,//点击遮罩层关闭
                    content: url,
                    success: function (layero, index) {
                        var body = layer.getChildFrame('body', index);

                    },
                    end: function () {
                        //层被销毁后刷新页面
                        location.reload();
                    }

                });
            }

            $("#add").on("click", function () {
                add("增加房间", 400, 400, "/roomDetail/roomAdd")
            });
        });
    };

</script>
<script type="text/html" id="edit">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<!--头部工具栏-->
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-danger" lay-event="delAll"><i class="layui-icon"></i>批量删除</button>

    </div>
</script>
</body>

</html>